﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Data;

namespace PresentationLayer
{
    public partial class MakeRequisition : System.Web.UI.Page
    {

        MakeRequistionController mrContoller;
        StationeryInventoryEntities1 ent;
        DataTable dtCurrentTable;
        DataRow drCurrentRow = null;
        //String userName = "CPSC_Emp2";
        String userName;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (Session["UserLoggedIn"] == null)
                {
                    Response.Redirect("login.aspx");
                }
                else
                {
                    List<String> loginValues = (List<String>)(Session["UserLoggedIn"]);
                    userName = loginValues[3];

                }

                mrContoller = new MakeRequistionController();
                ent = new StationeryInventoryEntities1();
                if (!IsPostBack)
                {

                    ddlCategory.DataSource = mrContoller.getCatList();
                    ddlCategory.DataTextField = "CategoryName";
                    ddlCategory.DataValueField = "CategoryID";
                    ddlCategory.DataBind();
                    ddlCategory.Items.Insert(0, "Choose Category...");


                    //---------------------for tempItems-----------------------
                    DataTable dt = new DataTable();

                    dt.Columns.Add("ItemCode", typeof(string));
                    dt.Columns.Add("ItemName", typeof(string));
                    dt.Columns.Add("Category", typeof(string));
                    dt.Columns.Add("Qty", typeof(int));

                    ViewState["CurrentTable"] = dt;

                    gvTempItemList.DataSource = dt;
                    gvTempItemList.DataBind();
                    //-----------------------end for tempItems------------------
                    btnSubmit.Visible = false;

                }

                // Block the buttons to prevent multiple clicks by the user
                btnSubmit.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnSubmit).ToString() + ";");
            }
            catch(Exception ex)
            {
                btnSubmit.Enabled = true;            
            }
           
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            try 
            {
                if (ddlCategory.SelectedIndex == 0)
                {

                }
                else
                {
                    int catId = Convert.ToInt32(ddlCategory.SelectedValue.ToString());
                    ddlStationery.DataSource = mrContoller.getStationeryListByCatID(catId);
                    ddlStationery.DataTextField = "ItemName";
                    ddlStationery.DataValueField = "ItemCode";
                    ddlStationery.DataBind();
                }
            }
            catch (Exception ex) { }
            
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
           // btnSubmit.Visible = true;
            try
            {
                lblError.Text = "";
                int checkNewItem = -1;

                if (ViewState["CurrentTable"] != null)
                {
                    dtCurrentTable = (DataTable)ViewState["CurrentTable"];

                    for (int i = 0; i < dtCurrentTable.Rows.Count; i++)
                    {
                        if (ddlStationery.SelectedValue == dtCurrentTable.Rows[i][0].ToString())
                        {
                            checkNewItem = i;
                        }
                    }
                }
                else
                {
                    dtCurrentTable = new DataTable();
                }

                if (checkNewItem == -1)
                {
                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow[0] = ddlStationery.SelectedValue;
                    drCurrentRow[1] = ddlStationery.SelectedItem.Text;
                    drCurrentRow[2] = ddlCategory.SelectedItem.Text;
                    drCurrentRow[3] = txtQty.Text;
                    dtCurrentTable.Rows.Add(drCurrentRow);
                }
                else
                {
                    int prevQty = Convert.ToInt32(dtCurrentTable.Rows[checkNewItem][3]);
                    dtCurrentTable.Rows[checkNewItem][3] = prevQty + Convert.ToInt32(txtQty.Text);
                }

                ViewState["CurrentTable"] = dtCurrentTable;
                gvTempItemList.DataSource = dtCurrentTable;
                gvTempItemList.DataBind();
                btnSubmit.Visible = true;
                //after adding, clear all field
                txtQty.Text = "";
                ddlCategory.SelectedIndex = 0;
                ddlStationery.Items.Clear();
            }
            catch (Exception ex)
            {
            }

            
           
            
        }

        protected void gvTempItemList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                if (ViewState["CurrentTable"] != null)
                {
                    dtCurrentTable = (DataTable)ViewState["CurrentTable"];
                    dtCurrentTable.Rows.RemoveAt(e.RowIndex);
                    gvTempItemList.DataSource = dtCurrentTable;
                    gvTempItemList.DataBind();
                }
            }
            catch (Exception ex)
            {

            }

           
         
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            //int reqID;
            try
            {
                if (gvTempItemList.Rows.Count == 0)
                {
                    lblError.Text = "Please Select Item to make requisition";
                }
                else
                {
                    String itemCode;
                    int reqQty;

                    //for requisition table
                    int reqBy = mrContoller.getEmpIDFromUserName(userName);
                    int reqID = mrContoller.makeRequisition(reqBy);

                    for (int i = 0; i < gvTempItemList.Rows.Count; i++)
                    {
                        itemCode = gvTempItemList.Rows[i].Cells[0].Text;
                        reqQty = Convert.ToInt32(gvTempItemList.Rows[i].Cells[3].Text);
                        mrContoller.makeRequistionDetail(reqID, itemCode, reqQty);
                    }


                    mrContoller.SendNotificationToDeptHead(userName, reqID);

                    Response.Redirect("~/RequisitionSuccess.aspx");
                }

                btnSubmit.Enabled = true;
                //string script = "alert('Your Stationery Requisition have been submitted successfully.\n An email has been sent to the Department Head!');";
                //ClientScript.RegisterStartupScript(this.GetType(), "Information", script, true);
            }

            catch (Exception ex)
            {
                btnSubmit.Enabled = true;
            }

        }  


        
        
    }
}